package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.OrderInfo;
import cn.wolfcode.shop.domain.OrderProduct;
import cn.wolfcode.shop.exception.UserException;
import cn.wolfcode.shop.message.OrderProducer;
import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.vo.GenerateOrderVo;
import cn.wolfcode.shop.vo.JSONResultVo;
import cn.wolfcode.shop.vo.OrderStatusChangeVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.sun.org.glassfish.external.statistics.annotations.Reset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wolfcode on 2018/8/20.
 */
@RestController
@RequestMapping("/api/orders")
public class OrderController {
    @Reference
    private IOrderInfoService orderInfoService;
    @Autowired
    private OrderProducer orderProducer;
    @PostMapping
    public JSONResultVo generateOrder(@RequestHeader("header-token") String token, @RequestBody GenerateOrderVo generateOrderVo){
        JSONResultVo vo = new JSONResultVo();
        try{
            Map<String,Object> map = new HashMap();
            map.put("token",token);
            map.put("generateOrderVo", JSON.toJSONString(generateOrderVo));
            orderProducer.sendMsg(map);
        }catch(UserException ex){
            ex.printStackTrace();;
            vo.setErrorMsg(ex.getMessage());
        }
        return vo;
    }
    @PutMapping
    public JSONResultVo changeOrderStatus(@RequestHeader("header-token") String token, @RequestBody OrderStatusChangeVo orderStatusChangeVo){
        JSONResultVo vo = new JSONResultVo();
        try{
            OrderInfo orderInfo = orderInfoService.changeStatus(token,orderStatusChangeVo);
            vo.setResult(orderInfo);
        }catch(UserException ex){
            ex.printStackTrace();;
            vo.setErrorMsg(ex.getMessage());
        }
        return vo;
    }
}
